package com.idanchuang.component.provider.api.util;

import com.zoi7.component.core.util.MD5Utils;

import java.util.List;

/**
 * @author yjy
 * @date 2019/11/21 10:11
 **/
public class RpcUtil {

    /**
     * 签名
     * @param path 访问path
     * @param secretKey key
     * @return 签名
     */
    public static String sign(String path, String secretKey) {
        path = path == null ? "" : path;
        secretKey = secretKey == null ? "" : secretKey;
        return MD5Utils.MD5Encode(path + secretKey);
    }

    /**
     * 验证签名
     * @param sign 签名
     * @param path 访问path
     * @param secretKeys 可接受的key
     * @return ..
     */
    public static boolean verifySign(String sign, String path, List<String> secretKeys) {
        if (sign == null) {
            return false;
        }
        if (secretKeys == null || secretKeys.isEmpty()) {
            return false;
        }
        for (String key : secretKeys) {
            if (sign.equalsIgnoreCase(sign(path, key))) {
                return true;
            }
        }
        return false;
    }

}
